package com.ray.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.RequestFacade;
import org.apache.catalina.realm.MessageDigestCredentialHandler;
import org.apache.commons.lang.StringUtils;
import org.apache.coyote.Request;
import org.apache.tomcat.util.buf.MessageBytes;
import org.junit.validator.PublicClassValidator;
import org.springframework.util.Assert;

import com.ray.service.BasicDataService;
import com.thoughtworks.xstream.mapper.Mapper.Null;

public class StringUtil {

	// 保留小数点位数
	public static String interceptDecimal(String str, int count) {
		int index = str.indexOf(".");
		if (index > 0) {
			str = str.substring(0, index + 1 + count);
		}
		return str;
	}

	public static String getUUID() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String uuid = sdf.format(new Date())
				+ UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}

	/**
	 * 字符串排序分组
	 * 
	 * @param paramSet
	 * @param returnList
	 * @return
	 */
	public static List<String> groupStr(Set<String> paramSet,
			List<String> returnList) {
		
		for(String str:paramSet) {
			List<String> groupList = new ArrayList<String>();
			groupList.add(str);
			for(String group:returnList) {
				groupList.add(group+"-"+str);
			}
			returnList.addAll(groupList);
		}
		return returnList;
//		
//		if (paramSet.size() <= 0) {
//			return returnList;
//		}
//		List<String> groupList = new ArrayList<String>();
//		List<String> list = new ArrayList<String>(paramSet);
//		String parem = list.get(0);
//		groupList.add(parem);
//		for (String str : returnList) {
//			groupList.add(str + "-" + parem);
//		}
//		returnList.addAll(groupList);
//		paramSet.remove(parem);
//		return groupStr(paramSet, returnList);
		
		
		
	};

	/**
	 * 微信跳转路径去除jsessionid
	 */
	public static String getWeiXinRedirectUrl(String url) {
		String replace = "";
		int ssionidIndex = url.indexOf(";jsessionid");
		if (ssionidIndex == -1) {
			return url;
		}
		String newUrl = new String(url);
		newUrl = newUrl.replace("?", "#");
		int paramIndex = newUrl.indexOf("#");
		if (paramIndex == -1) {
			replace = url.substring(url.indexOf(";jsessionid"), url.length());
		} else {
			replace = url.substring(url.indexOf(";jsessionid"), paramIndex);
		}
		System.out.println(replace);
		return url.replaceFirst(replace, "");
	}

	public static void setRequestUrl(ServletRequest arg0,
			FilterChain filterchain, ServletResponse arg1)
			throws NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException, IOException,
			ServletException {
		RequestFacade facade = (RequestFacade) arg0;
		Class clzz = RequestFacade.class;
		// try {
		Field field = clzz.getDeclaredField("request");
		field.setAccessible(true);
		org.apache.catalina.connector.Request request = (org.apache.catalina.connector.Request) field
				.get(facade);
		Class requestClass = request.getClass();
		// 获取封装org.apache.coyote.Request的字段
		Field coyoteField = requestClass.getDeclaredField("coyoteRequest");
		coyoteField.setAccessible(true);
		Request coyoteRequest = (Request) coyoteField.get(request);
		Class requestClazz = Request.class;
		// 获取org.apache.coyote.Request中保存路径的字段
		Field uriMBField = requestClazz.getDeclaredField("uriMB");
		uriMBField.setAccessible(true);
		MessageBytes uriMB = (MessageBytes) uriMBField.get(coyoteRequest);
		// 这里就是改变路径的地方
		String path = uriMB.toString();
		System.out.println(path);
		path = StringUtil.getWeiXinRedirectUrl(path);
		uriMB.setString(path);
		// 用来打印请求路径
		// System.out.println(((RequestFacade) arg0).getRequestURL());
		// } catch (NoSuchFieldException e) {
		// e.printStackTrace();
		// } catch (IllegalAccessException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// } catch (ServletException e) {
		// e.printStackTrace();
		// }
	}

	/**
	 * 判断字符串是否包含中文汉字与中文符号
	 * 
	 * @param str
	 * @return true包含 flase不含
	 * @author 王荣
	 */
	public static boolean isContainChinese(String str) {
		if (StringUtils.isEmpty(str)) {
			return false;
		}
		Pattern pattern = Pattern
				.compile("\"[\\u4E00-\\u9FA5|\\\\！|\\\\，|\\\\。|\\\\（|\\\\）|\\\\《|\\\\》|\\\\“|\\\\”|\\\\？|\\\\：|\\\\；|\\\\【|\\\\】]\"");
		Matcher matcher = pattern.matcher(str);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @Title: isEmpty
	 * @author: 李宇航
	 * @Description: 判断字符串是否为空或者null，是返回true，否则返回false
	 * @param str
	 * @return: boolean
	 * @throws
	 */
	public static boolean isEmpty(String str) {
		if (null == str || "".equals(str.trim())) {
			return true;
		}
		return false;
	}
	
	public static boolean checkEqu(String obj,String checkObj) {
		if(obj==null&&checkObj==null) {
			return true;
		}
		if(obj !=null && checkObj!=null) {
			return obj.equals(checkObj);
		}
		return false;
	}
	
	
	
	/**
     * 
     * @param map
     * @param param
     * @param returnStr
     */
	public static  void checkNotNull(Map map, String param, String returnStr) {
		Assert.isTrue(map.get(param)!=null,returnStr);
	}

	/**
	 * 校验参数isTrue
	 * @param map
	 * @param param(参数名)
	 * @param returnStr(返回提示信息)
	 */
	public static void checkIsTrue(Map map, String param, String returnStr) {
		Assert.isTrue(map.get(param)!=null && !"".equals(map.get(param).toString()),returnStr);
	}
}
